<script setup lang='ts'>
import { ref, reactive } from 'vue'
import DetailDialog from './DetailDialog.vue'

import { 
  addPermission, 
  editPermission, 
  deletePermission 
} from '@/api/user'

defineProps({
  showDialog: {
    type: Function,
    default: () => {}
  },
  permissionList: {
    type: Array,
    default: () => {}
  }
})
// refs
const detailDialogRef = ref()
// 表格
const tableData = [
  {
    id: 1,
    date: '2016-05-02',
    name: 'wangxiaohu',
    address: 'No. 189, Grove St, Los Angeles'
  },
  {
    id: 2,
    date: '2016-05-04',
    name: 'wangxiaohu',
    address: 'No. 189, Grove St, Los Angeles'
  },
  {
    id: 3,
    date: '2016-05-01',
    name: 'wangxiaohu',
    address: 'No. 189, Grove St, Los Angeles',
    children: [
      {
        id: 31,
        date: '2016-05-01',
        name: 'wangxiaohu',
        address: 'No. 189, Grove St, Los Angeles'
      },
      {
        id: 32,
        date: '2016-05-01',
        name: 'wangxiaohu',
        address: 'No. 189, Grove St, Los Angeles'
      }
    ]
  },
  {
    id: 4,
    date: '2016-05-03',
    name: 'wangxiaohu',
    address: 'No. 189, Grove St, Los Angeles'
  }
]

const cellStyle = {
  textAlign: 'center'
}

const headStyle = {
  textAlign: 'center'
}

// 分页
const pageInfo = ref({
  total: 100,
  pageSize: 10,
  pageNum: 1
})

const handlePlanSizeChange = (val: number) => {
  console.log(`${val} items per page`)
}
const handlePlanCurrentChange = (val: number) => {
  console.log(`current page: ${val}`)
}

// 展示详细信息弹框
const showDetailDialog = (row:any) => {
  console.log('展示详情')
  detailDialogRef.value.showDialog(row)
}
</script>

<template>
  <el-table
    :cell-style="cellStyle"
    :header-cell-style="headStyle"
    :data="permissionList"
    max-height="620"
  >
    <el-table-column
      prop="name"
      label="菜单名称"
    />
    <el-table-column
      prop="sortIndex"
      label="排序"
    />
    <el-table-column
      prop="path"
      label="路由路径"
    />
    <el-table-column
      prop="componetPath"
      label="组件路径"
    />
    <el-table-column
      prop="address"
      label="图标"
    >
      <template #default="scope">
        <span>{{ scope.row.icon }}</span>
      </template>
    </el-table-column>
    <el-table-column
      align="center"
      label="详情"
    >
      <template
        #default="scope"
      >
        <el-link
          v-if="!scope.row.children || scope.row.children.length == 0"
          type="primary"
          @click="showDetailDialog(scope.row)"
        >
          详细信息
        </el-link>
      </template>
    </el-table-column>
    <el-table-column
      prop="name"
      label="操作"
    >
      <template #default="scope">
        <div class="btn-box">
          <div
            class="add-btn"
            @click="showDialog(1, scope.row)"
          >
            添加子菜单
          </div>
          <div
            class="update-btn"
            @click="showDialog(2,scope.row)"
          >
            编辑
          </div>
          <div class="delete-btn">
            删除
          </div>
        </div>
      </template>
    </el-table-column>
  </el-table>
  <div class="pagination-box">
    <el-pagination
      v-model:current-page="pageInfo.pageNum"
      v-model:page-size="pageInfo.pageSize"
      :page-sizes="[5, 10, 15, 20]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total"
      @size-change="handlePlanSizeChange"
      @current-change="handlePlanCurrentChange"
    />
  </div>
  <DetailDialog ref="detailDialogRef" />
</template>

<style scoped lang=scss>
.btn-box {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;

  >div {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 56px;
    height: 30px;
    color: #fff;
    border-radius: 5px;

    /* background-color: red; */

    &:hover {
      cursor: pointer;
    }
  }

  .add-btn {
    width: 102px;
    height: 30px;
    font-size: 12px;
    background-color: #85ce61;
  }

  .update-btn {
    margin-left: 10px;
    font-size: 12px;
    background-color: #e6a23c;
  }

  .delete-btn {
    margin-left: 10px;
    font-size: 12px;
    background-color: #f56c6c;
  }
}

.pagination-box {
  margin-top: 10px;
}
</style>